<?php
namespace Admin\Controller;

class DashboardController extends BaseController {


	public function index() {


        $dateArr = getTenDate();
        // 此处获取访问量
        $Visit = D ('Visit');
        $Attend = D ('Attend');
        // $visitData = array();
        // $attendData = array();
        // foreach ($dateArr as $k=>$v) {
        //     $visitData[$k]['period'] = $attendData[$k]['period'] = $v['y'].'-'.$v['m'].'-'.$v['d'];
        //     $visitData[$k]['teacher'] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],1);
        //     $visitData[$k]['student'] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],0);
            
        //     $attendData[$k]['kk'] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],1);
        //     $attendData[$k]['cd'] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],2);
        //     $attendData[$k]['qj'] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],3);
        //     $attendData[$k]['zt'] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],4);
        // }
        
        // 访问量
        $sVisitArr = array();
        $tVisitArr = array();
        // 出勤量
        $kkArr = array();
        $cdArr = array();
        $qjArr = array();
        $ztArr = array();
        // 日期
        $date = array();
        foreach (array_reverse($dateArr) as $k=>$v) {
            $sVisitArr[] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],0);
            $tVisitArr[] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],1);

            $kkArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],1);
            $cdArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],2);
            $qjArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],3);
            $ztArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],4);

            $date[] = $v['y'] . '/' . $v['m'] . '/' . $v['d'];
        }

        // 访问量数据
        $visitData = array(
            array("name"=>"教师","data"=>$tVisitArr),
            array("name"=>"学生","data"=>$sVisitArr),

        );

        // 出勤数据
        $attendData = array(
            array("name"=>"旷课","data"=>$kkArr),
            array("name"=>"迟到","data"=>$cdArr),
            array("name"=>"请假","data"=>$qjArr),
            array("name"=>"早退","data"=>$ztArr)
        );

        // 将日期信息存入session
        session('vdate',json_encode($dateArr));
        session('adate',json_encode($dateArr));

        $this->assign('date',json_encode($date));
        $this->assign('visitData',json_encode($visitData));
        $this->assign('attendData',json_encode($attendData));
        $this->display();
    }

    // 退出登录
    public function loginout() {
        session('admin',null);
        echo "<script>location.reload();</script>";
    }


    /**
     * 刷新流量图表
     * @return json
     */
    public function refreshVisitChart() {

        if (IS_AJAX) {

            $vdate = json_decode(session('vdate'),true);
            $type = I ("request.type");
           // p($type);
            if (empty($vdate) || empty($type))
                $this->out("RESULT_FAIL","缺少参数");


            switch ($type) {
                case "last":
                    $dateArr = getTenDate($type,$vdate[9]['y'] .'/'. $vdate[9]['m'] .'/'. $vdate[9]['d']);
                    break;
                case 'next':
                    $dateArr = getTenDate($type,$vdate[0]['y'] .'/'. $vdate[0]['m'] .'/'. $vdate[0]['d']);
                    break;
                default:
                    $dateArr = getTenDate();
            }

            // 此处获取访问量
            $Visit = D ('Visit');
            $sVisitArr = array();
            $tVisitArr = array();
            $date = array();
            foreach (array_reverse($dateArr) as $k=>$v) {
                //$visitData[$k]['period'] = $v['y'].'-'.$v['m'].'-'.$v['d'];
                //$visitData[$k]['teacher'] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],1);
                //$visitData[$k]['student'] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],0);
                $sVisitArr[] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],0);
                $tVisitArr[] = $Visit->getDayVisit($v['y'],$v['m'],$v['d'],1);
                $date[] = $v['y'] . '/' . $v['m'] . '/' . $v['d'];
            }

            $data = array(
                array("name"=>"教师","data"=>$tVisitArr),
                array("name"=>"学生","data"=>$sVisitArr),
            );

            // 最终返回结果，包括日期和数据
            $result = array(
                "date" => $date,
                "data" => $data
            );

            // 将日期信息存入session
            session('vdate',json_encode($dateArr));

            $this->out("RESULT_OK","查询成功",$result);
        }

    }


    public function refreshAttendChart() {
        if (IS_AJAX) {

            $adate = json_decode(session('adate'),true);
            $type = I ("request.type");
           // p($type);
            if (empty($adate) || empty($type))
                $this->out("RESULT_FAIL","缺少参数");


            switch ($type) {
                case "last":
                    $dateArr = getTenDate($type,$adate[9]['y'] .'/'. $adate[9]['m'] .'/'. $adate[9]['d']);
                    break;
                case 'next':
                    $dateArr = getTenDate($type,$adate[0]['y'] .'/'. $adate[0]['m'] .'/'. $adate[0]['d']);
                    break;
                default:
                    $dateArr = getTenDate();
            }

            // 此处获取访问量
            $Attend = D ('Attend');
            $kkArr = array();
            $cdArr = array();
            $qjArr = array();
            $ztArr = array();
            $date = array();
            foreach (array_reverse($dateArr) as $k=>$v) {
                
                $kkArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],1);
                $cdArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],2);
                $qjArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],3);
                $ztArr[] = $Attend->getDayAttend($v['y'],$v['m'],$v['d'],4);

                $date[] = $v['y'] . '/' . $v['m'] . '/' . $v['d'];
            }

            // 出勤数据
            $data = array(
                array("name"=>"旷课","data"=>$kkArr),
                array("name"=>"迟到","data"=>$cdArr),
                array("name"=>"请假","data"=>$qjArr),
                array("name"=>"早退","data"=>$ztArr)
            );

            // 最终返回结果，包括日期和数据
            $result = array(
                "date" => $date,
                "data" => $data
            );

            // 将日期信息存入session
            session('adate',json_encode($dateArr));

            $this->out("RESULT_OK","查询成功",$result);
        }
    }



    public function get_server_info() {

        $this->verify_request();

        // 获取分页数据
        $page = $_POST['page'];
        $rows = $_POST['rows'];

        $Mysql = D ('Mysql');
        $ServerConfig = M('ServerConfig');
        $count = $ServerConfig->count();
        $res = $ServerConfig->limit(($page-1)*$rows .','.$rows)->select();

        $data = array();
        foreach ($res as $k=>$v) {
            $data[$k]['name'] = $v['name'];
            $data[$k]['value'] = $v['value'];
        }

        $this->ajaxReturn(array('total'=>$count,'rows'=>$data));
    }

    public function refresh_server_info() {

        $Mysql = D('Mysql');
        $ServerConfig = M ('ServerConfig');
        // 清空原数据
        $ServerConfig->where('1')->delete();
        // 重新获取服务器配置
        $serverinfo = array(
            array('name'=>'服务器标识','value'=>$_SERVER["SERVER_SOFTWARE"]),
            array('name'=>'服务器主机名','value'=>$_SERVER['SERVER_NAME']),
            array('name'=>'服务器系统类型及版本号','value'=>php_uname()),
            array('name'=>'服务器语言','value'=>$_SERVER['HTTP_ACCEPT_LANGUAGE']),
            array('name'=>'服务器WEB端口','value'=>$_SERVER['SERVER_PORT']),
            array('name'=>'服务器时间','value'=>date("Y-m-d H:i:s", time())),
            array('name'=>'服务器允许最大上传附件','value'=>ini_get("file_uploads") ? ini_get("upload_max_filesize") : "Disabled"),
            array('name'=>'服务器脚本最大执行时间','value'=>ini_get("max_execution_time")."秒"),
            array('name'=>'服务器PHP版本','value'=>PHP_VERSION),
            array('name'=>'服务器数据库版本','value'=>$Mysql->getMysqlVersion())
        );

        foreach ($serverinfo as $val) {
            $data['name'] = $val['name'];
            $data['value'] = $val['value'];
            $ServerConfig->add($data);
        }

        $this->ajaxReturn(array('success'=>true));
    }
    

}